package com.tianqu.tms.www.cardect.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.viewhelper.CarWDPersonTopListViewHelper;

/**
 * 
 * 待检测（人工）转Ipad录入Action
 * 
 * @author tianshifu
 * 
 */
public class CarWDPersonToIpadConfirmController extends TmsController {

	private static final String ALLOCATEID = "carSerial";// 定义页面checkbox的property
	private static final String ADDMODE = "addModel";// 转录入模式 3 人工

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String addMode = (String) uc.getAttribute(ADDMODE);// 获取录入模式

			String[] carSerial = (String[]) uc.getAttribute(ALLOCATEID);// 获取选中ID

			TmsDetectInfo car = TmsDetectInfo.getInstance();

			// 人工 向entity层传一个车辆ID数组和更新的状态
			List<TmsDetectInfo> carList = car.findAllocateInfoByCheckbox(
					carSerial, addMode);// 根据车辆ID查询车辆信息
			List<CarWDPersonTopListViewHelper> viewList = new ArrayList<CarWDPersonTopListViewHelper>();
			CarWDPersonTopListViewHelper view = new CarWDPersonTopListViewHelper();
			if (carList.size() > 0) {
				for (int i = 0; i < carList.size(); i++) {
					TmsDetectInfo entity = (TmsDetectInfo) carList.get(i);
					// 将entity数据转型放到viewhelp中
					view = new CarWDPersonTopListViewHelper();
					view = setViewFromEntity(entity, view);
					viewList.add(view);
				}
			}

			view.setWdPersonConfirmList(viewList);
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 重构下list 将数据库数据转化成页面需要的数据
	 * 
	 * @throws TmsDaoException
	 */
	private CarWDPersonTopListViewHelper setViewFromEntity(
			TmsDetectInfo entity, CarWDPersonTopListViewHelper view)
			throws TmsDaoException {
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setRegDate(entity.getRegDate());
		view.setCarCheckInsur(entity.getCarCheckInsur());
		view.setTrafficInsur(entity.getTrafficInsur());
		view.setColor(entity.getColor());
		view.setUpdateTime(entity.getUpdateTime());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));// 毫升转成升+“L”
		DectCodes code = DectCodes.getInstance();

		view.setGearbox(code.getItemValueByItemIdAndItemCode(
				DectCodes.GEARBOX_TYPE, String.valueOf(entity.getGearbox())));
		view.setFuelDict(code.getItemValueByItemIdAndItemCode(
				DectCodes.FUEL_TYPE, String.valueOf(entity.getFuelDict())));
		view.setNature(code.getItemValueByItemIdAndItemCode(
				DectCodes.NATURE_TYPE, String.valueOf(entity.getNature())));
		view.setCarModeName(entity.getAddModeName());// 录入方式名
		// 设置页面检测模式显示
		if (entity.getChkModel() == 0) {
			// 未检测
			view.setChkModel("-");
		} else {
			view.setChkModel(code
					.getItemValueByItemIdAndItemCode(DectCodes.DETECT_MODE,
							String.valueOf(entity.getChkModel())));
		}

		// view.setSourse(code.getItemValueByItemIdAndItemCode(DectCodes.CAR_SOURSE,
		// String.valueOf(entity.getSource())));//数据来源

		return view;
	}
}
